

package org.t2.design.module.autogen;


public abstract class CheckAbstractTable<T extends CheckAbstractTable.CheckCfg> extends org.gaming.design.loader.DesignCache<T> {

	@Override
	public final String fileName() {
		return "C-自动检测表";
	}
	
	
	protected java.util.Map<String, T> IdIndex;
	
	/**
	 * 从索引中获取对象，获取不到会抛异常
	 */
	public final T getInIdIndex(
		int Id
	) {
		String key = "" + Id;
		T t = IdIndex.get(key);
		if(t == null) {
			throw new org.gaming.design.exception.DesignNotFoundException("CheckTable when getInIdIndex", key);
		}
		return t;
	}
	
	/**
	 * 从索引中查找对象，获取不到会返回null
	 */
	public final T findInIdIndex(
		int Id
	) {
		String key = "" + Id;
		return IdIndex.get(key);
	}
	
	/**
	 * 加载IdIndex索引
	 */
	private final void loadIdIndex() {
		java.util.Map<String, T> IdIndex = new java.util.HashMap<>();
		for(T data : all()) {
			String[] keys = new String[] {
				String.valueOf(data.getId()),
			};
			String key = String.join("_", keys);
			if(IdIndex.containsKey(key)) {
				logger.error("{} found duplicate key {} in index {}", fileName(), key, IdIndex);
				super.hasError = true;
			}
			IdIndex.put(key, data);
		}
		this.IdIndex = IdIndex;
	}
	
	
	
	@Override
	protected final void loadAutoGenerate() {
		//加载索引
		loadIdIndex();
	
		
	}
	
	public static class CheckCfg {
		//物品id
		protected int Id;
		/**
		 * get物品id
		 */
		public int getId() {
			return Id;
		}
		//物品名称
		protected String Name;
		/**
		 * get物品名称
		 */
		public String getName() {
			return Name;
		}
		//单次获取数量
		protected int GetNum;
		/**
		 * get单次获取数量
		 */
		public int getGetNum() {
			return GetNum;
		}
		//库存总数量
		protected int TotalNum;
		/**
		 * get库存总数量
		 */
		public int getTotalNum() {
			return TotalNum;
		}
	}

}